package com.tl.juc.c5;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/***
 * @Author 徐庶
 * @Slogan 致敬大师，致敬未来的你
 */
public class C0_CountDownLatch {

    public static void main(String[] args) {
        Semaphore semaphore1 = new Semaphore(1); // 初始许可为1
        Semaphore semaphore2 = new Semaphore(0); // 初始许可为0

        Thread t1 = new Thread(() -> {
            try {
                semaphore1.acquire(); // 获取许可
                System.out.println("Thread 1 is executing.");
                semaphore2.release(); // 释放许可，允许T2执行
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                semaphore2.acquire(); // 获取许可，确保T1执行完
                System.out.println("Thread 2 is executing.");
                semaphore1.release(); // 释放许可，允许T3执行
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            try {
                semaphore1.acquire(); // 获取许可，确保T2执行完
                System.out.println("Thread 3 is executing.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }
}